package com.vcc.web.controller;


import cn.dev33.satoken.annotation.SaCheckPermission;
import cn.hutool.core.collection.ListUtil;
import com.mybatisflex.core.paginate.Page;
import com.vcc.core.annotations.RestPack;
import com.vcc.data.entity.Role;
import com.vcc.data.entity.TransferOrder;
import com.vcc.data.entity.User;
import com.vcc.service.client.vo.wellat.TransferOrderVo;
import com.vcc.service.service.TransferOrderService;
import com.vcc.service.service.UserService;
import com.vcc.web.core.util.UserUtil;
import jakarta.annotation.Resource;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping("transferOrder")
public class TransferOrderController {

    @Resource
    private TransferOrderService transferOrderService;
    @Resource
    private UserService userService;


    @GetMapping("paging")
    @SaCheckPermission(value = "wallet:transfer:view")
    @RestPack
    public Page<TransferOrderVo> paging(
            Integer pageNumber,
            Integer pageSize,
            String orderId,
            String type,
            Long targetUserId,
            String startDate,
            String endDate
    ) {
        User user = UserUtil.get();
        Long parentId = user.getParentId();
        Role role = UserUtil.getRole();
        Page<TransferOrder> paging = transferOrderService.paging(
                pageNumber,
                pageSize,
                role, // Assuming Role is not needed here, adjust as necessary
                orderId,
                type,
                role.getHasAllData() && parentId != null && parentId != 0 ? parentId : user.getId(),
                targetUserId,
                startDate,
                endDate
        );
        List<TransferOrder> records = paging.getRecords();
        if (records == null || records.isEmpty()) {
            return Page.of(
                    paging.getPageNumber(),
                    paging.getPageSize()
            );
        }
        List<Long> userIds = records.stream()
                .map(TransferOrder::getUserId)
                .distinct()
                .toList();
        List<Long> operateIds = records.stream().map(TransferOrder::getOperateId)
                .filter(operateId -> operateId != null && operateId > 0)
                .distinct()
                .toList();
        List<User> users = userService.listByIds(userIds);
        List<User> operateUsers = operateIds.isEmpty() ? ListUtil.empty() : userService.listByIds(operateIds);
        return paging.map(it -> {
            User accountUser = users.stream()
                    .filter(user1 -> user1.getId().equals(it.getUserId()))
                    .findFirst()
                    .orElse(null);
            User operateUser = operateUsers.stream()
                    .filter(user1 -> user1.getId().equals(it.getOperateId()))
                    .findFirst()
                    .orElse(null);
            return new TransferOrderVo(
                    it,
                    accountUser != null ? accountUser.getAccount() : null,
                    operateUser != null ? operateUser.getAccount() : null
            );
        });
    }


}
